﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Timesheet.Service;
using Timesheet.Domain;

namespace Timesheet.WebForm.Report
{
    public partial class report : System.Web.UI.Page
    {
        public IProjectService _PrjService { get; set; }

        public IPrjReferencesService _PrjReferencesService { get; set; }

        public ITeamService _TeamService { get; set; }

        public ITimeSheetsService _TmshService { get; set; }

        private rpttmsh reportView = new rpttmsh();
        private rptProject project;
        private List<rptTimeSheetDetails> tmhsList;


        protected void Page_Load(object sender, EventArgs e)
        {
            Site myMasterPage = Page.Master as Site;
            myMasterPage._MainPanel.Width = 1169 + 100;
            if (!IsPostBack)
            {
                if (Session["User"] == null)
                    Server.Transfer("~/Account/Login.aspx");
                Session.Remove("IsReport");
                Session.Remove("IsView");
            }
            ViewReport();
        }

        DateTime beginDate = new DateTime(1, 1, 1);
        DateTime endDate = new DateTime(9999, 12, 31);
        protected void btnView_Click(object sender, EventArgs e)
        {
            Session["IsView"] = true;
            Session["IsReport"] = true;
            docView.Report = new rpttmsh();
        }
        void ViewReport()
        {
            Session.Remove("IsReport");
            string from = beginDate.ToString("yyyyMMdd");
            long tmshID = Convert.ToInt64(Request.Params["tmshID"]);
            var _currentTmsh = _TmshService.GetByID(tmshID);
            long prjID = _currentTmsh.ProjectID;
            Team team = _TeamService.GetByID(_currentTmsh.TeamID);
            string location = "";
            if(team != null)
            {
                PrjReferences pr = _PrjReferencesService.GetByRefId(team.LocationID);
                if(pr != null)
                    location = pr.RefNames;
            }

            var data = _PrjService.GetReportData(prjID, tmshID);

            reportView = new rpttmsh();
            dsReport ds = new dsReport();

            project = data[0] as rptProject;
            tmhsList = data[1] as List<rptTimeSheetDetails>;

            docView.Report = CreateReport(tmhsList, project, _currentTmsh, location);
        }

        rpttmsh CreateReport(List<rptTimeSheetDetails> lst, rptProject project, Domain.TimeSheets _currentTmsh,string location)
        {

            var report = new rpttmsh();
            report.setHeaderForReport(project, _currentTmsh, location);
            report.DataSource = ConvertToDataSet(lst);
            report.CreateDocument();
            return report;
        }

        dsReport ConvertToDataSet(List<rptTimeSheetDetails> lst)
        {
            var ds = new dsReport();

            foreach (var item in tmhsList)
            {
                var row = ds.Tables[0].NewRow();
                row[0] = item.ClassCode;
                row[1] = item.Mileage;
                row[2] = item.PerDiemFull;
                row[3] = item.PerDiemMeals;
                row[4] = item.SunOTHours;
                row[5] = item.SunSTHours;
                row[6] = item.MonOTHours;
                row[7] = item.MonSTHours;
                row[8] = item.TueOTHours;
                row[9] = item.TueSTHours;
                row[10] = item.WebOTHours;
                row[11] = item.WebSTHours;
                row[12] = item.ThuOTHours;
                row[13] = item.ThuSTHours;
                row[14] = item.FirOTHours;
                row[15] = item.FriSTHours;
                row[16] = item.SatOTHours;
                row[17] = item.SatSTHours;
                row[18] = item.TotalOT;
                row[19] = item.TotalST;
                row[20] = item.Extension;
                row[21] = item.Notes;
                row[22] = item.FullName;
                row[23] = item.Note1;
                row[24] = item.Note2;
                row[25] = item.Note3;
                row[26] = item.Note4;
                row[27] = item.Note5;
                row[28] = item.Note6;
                row[29] = item.Note7;
                row[30] = item.Group;
                ds.Tables[0].Rows.Add(row);
            }
            return ds;
        }
    }
}